package com.uniflow.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDateTime;
import java.util.List;

/**
 * API应用实体类
 * 
 * @author uniflow
 * @since 1.0.0
 */
@ApiModel(description = "API应用")
@TableName("api_applications")
public class ApiApplication {
    
    @ApiModelProperty("应用ID")
    @TableId(value = "id", type = IdType.ASSIGN_ID)
    private String id;
    
    @ApiModelProperty("应用名称")
    @TableField("app_name")
    private String appName;
    
    @ApiModelProperty("应用描述")
    @TableField("description")
    private String description;
    
    @ApiModelProperty("联系邮箱")
    @TableField("contact_email")
    private String contactEmail;
    
    @ApiModelProperty("IP白名单")
    @TableField("allowed_ips")
    private String allowedIps;
    
    @ApiModelProperty("状态：1启用 0禁用")
    @TableField("status")
    private Integer status;
    
    @ApiModelProperty("创建时间")
    @TableField("created_at")
    private LocalDateTime createdAt;
    
    // 非数据库字段
    @ApiModelProperty("API密钥列表")
    @TableField(exist = false)
    private List<ApiKey> apiKeys;
    
    @ApiModelProperty("IP白名单列表")
    @TableField(exist = false)
    private List<String> allowedIpList;
    
    @ApiModelProperty("调用统计")
    @TableField(exist = false)
    private ApiApplicationStats stats;
    
    // 构造函数
    public ApiApplication() {
        this.status = 1;
        this.createdAt = LocalDateTime.now();
    }
    
    public ApiApplication(String appName, String description, String contactEmail) {
        this();
        this.appName = appName;
        this.description = description;
        this.contactEmail = contactEmail;
    }
    
    // Getter和Setter方法
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getAppName() {
        return appName;
    }
    
    public void setAppName(String appName) {
        this.appName = appName;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public String getContactEmail() {
        return contactEmail;
    }
    
    public void setContactEmail(String contactEmail) {
        this.contactEmail = contactEmail;
    }
    
    public String getAllowedIps() {
        return allowedIps;
    }
    
    public void setAllowedIps(String allowedIps) {
        this.allowedIps = allowedIps;
    }
    
    public Integer getStatus() {
        return status;
    }
    
    public void setStatus(Integer status) {
        this.status = status;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public List<ApiKey> getApiKeys() {
        return apiKeys;
    }
    
    public void setApiKeys(List<ApiKey> apiKeys) {
        this.apiKeys = apiKeys;
    }
    
    public List<String> getAllowedIpList() {
        return allowedIpList;
    }
    
    public void setAllowedIpList(List<String> allowedIpList) {
        this.allowedIpList = allowedIpList;
    }
    
    public ApiApplicationStats getStats() {
        return stats;
    }
    
    public void setStats(ApiApplicationStats stats) {
        this.stats = stats;
    }
    
    // 业务方法
    
    /**
     * 判断是否启用
     */
    public boolean isEnabled() {
        return status != null && status == 1;
    }
    
    /**
     * 检查IP是否在白名单中
     */
    public boolean isIpAllowed(String ip) {
        if (allowedIps == null || allowedIps.trim().isEmpty()) {
            return true; // 没有限制则允许所有IP
        }
        
        String[] ips = allowedIps.split(",");
        for (String allowedIp : ips) {
            if (allowedIp.trim().equals(ip) || allowedIp.trim().equals("*")) {
                return true;
            }
        }
        return false;
    }
    
    /**
     * 获取状态描述
     */
    public String getStatusDesc() {
        return status != null && status == 1 ? "启用" : "禁用";
    }
    
    @Override
    public String toString() {
        return "ApiApplication{" +
                "id='" + id + '\'' +
                ", appName='" + appName + '\'' +
                ", description='" + description + '\'' +
                ", contactEmail='" + contactEmail + '\'' +
                ", status=" + status +
                ", createdAt=" + createdAt +
                '}';
    }
    
    /**
     * API应用统计信息内部类
     */
    public static class ApiApplicationStats {
        private Long totalCalls;
        private Long todayCalls;
        private Long successCalls;
        private Long errorCalls;
        private Double successRate;
        private Long avgResponseTime;
        
        // Getter和Setter方法
        public Long getTotalCalls() {
            return totalCalls;
        }
        
        public void setTotalCalls(Long totalCalls) {
            this.totalCalls = totalCalls;
        }
        
        public Long getTodayCalls() {
            return todayCalls;
        }
        
        public void setTodayCalls(Long todayCalls) {
            this.todayCalls = todayCalls;
        }
        
        public Long getSuccessCalls() {
            return successCalls;
        }
        
        public void setSuccessCalls(Long successCalls) {
            this.successCalls = successCalls;
        }
        
        public Long getErrorCalls() {
            return errorCalls;
        }
        
        public void setErrorCalls(Long errorCalls) {
            this.errorCalls = errorCalls;
        }
        
        public Double getSuccessRate() {
            return successRate;
        }
        
        public void setSuccessRate(Double successRate) {
            this.successRate = successRate;
        }
        
        public Long getAvgResponseTime() {
            return avgResponseTime;
        }
        
        public void setAvgResponseTime(Long avgResponseTime) {
            this.avgResponseTime = avgResponseTime;
        }
    }
}